/*入口文件作用：
 * 1、检查版本更新、网络状态
 * 2、检查本地存储判断是否新APP
 * 		首次打开显示轮播
 * 3、检查本地存储判断是否登录
 * 		a、若登录则启动父框架
 * 		b、未登录启动注册、登录页
 * */
//检查网络状态
//if(isallpay()) {
//	mui('.other')[0].style.display = 'none';
//}

if(functionclass.getnet()) {
	gothis()
} else {
	plus.navigator.closeSplashscreen();
	mui.alert('您的设备没有链接网络，请开启后点击重试。', configs.AppName, '重试', function() {
		plus.runtime.restart();
	});
}

function gothis() {
	if(configs.user_id) {
		mui.ajax(getApiUrl('http://www.jiaoyou0.cn', 'user/userDetail/'), {
			dataType: 'json', //服务器返回json格式数据
			type: 'get', //HTTP请求类型
			timeout: 5000, //超时时间设置为10秒；
			success: function(data) {
				plus.storage.setItem('user_info', JSON.stringify(data.data))
			},
			error: function(xhr, type, errorThrown) {
				mui.alert('网络不太好哦。', configs.AppName, '重试', function() {
					plus.runtime.restart();
				});
			}
		});

		mui.openWindow({
			url: "ParentFrame.html",
			id: "ParentFrame",
			show: {
				autoShow: true,
				aniShow: 'none',
			}
		})

	} else {
		var device = plus.os.name == 'iOS' ? 1 : 2;
		mui.get("http://jiaoyou1.cn/?s=down/installed&manager_id=" + configs.manager_id + "&device=" + device, '', function() {}, 'text');
		document.getElementById("login").style.display = "block";
		setTimeout(function() {
			plus.navigator.closeSplashscreen()
		}, 500)
	}
}

var auths = null;

function plusReady() {
	plus.oauth.getServices(function(services) {
		auths = services
	}, function(e) {
		alert("获取登录服务列表失败：" + e.message + " - " + e.code)
	})
}
document.addEventListener('plusready', plusReady, false);
// 登录操作
mui('.mui-content').on('tap', '#qq', function() {
	authLogin('qq')
})
mui('.mui-content').on('tap', '#weixin', function() {
	authLogin('weixin')
})

function authLogin(type) {
	var s;
	for(var i = 0; i < auths.length; i++) {
		console.log('auths.length', auths.length)
		console.log('auths[i].id', auths[i].id)
		if(auths[i].id == type) {
			s = auths[i];
			break;
		}
	}
	if(!s.authResult) {
		s.login(function(e) {
			authUserInfo(type);
			plus.nativeUI.closeWaiting();
		}, function(e) {
			plus.nativeUI.closeWaiting();
			mui.toast('登录失败！')
		});
	} else {
		mui.toast('已登录！')
	}
}

//注销
function authLogout() {
	for(var i in auths) {
		var s = auths[i];
		if(s.authResult) {
			s.logout(function(e) {}, function(e) {});
		}
	}
}

// 微信登录认证信息
function authUserInfo(type) {
	var s;
	for(var i = 0; i < auths.length; i++) {
		if(auths[i].id == type) {
			s = auths[i];
			break;
		}
	}
	if(!s.authResult) {
		alert("未授权登录！");
	} else {
		s.getUserInfo(function(e) {
			var josnStr = JSON.stringify(s.userInfo);
			var jsonObj = s.userInfo;
			s.userInfo.openid = s.authResult.openid;
			alert(s.authResult.openid)
			login(type, s.userInfo);
			authLogout();
		}, function(e) {
			alert("获取用户信息失败：" + e.message + " - " + e.code);
		});
	}
}

function login(type, data) {
	if(type == 'qq') var interface = 'User/qqLogin';
	if(type == 'weixin') var interface = 'User/wxLogin';
	var url = getApiUrl('http://www.jiaoyou0.cn', interface);
	mui.ajax({
		url: url,
		async: false,
		type: 'POST',
		data: data,
		error: function(err) {},
		success: function(ret) {
			var data = JSON.parse(ret);
			if(data.code == 1) {
				logindata(data.data, 'login');
			}

		}
	});
}

mui('.dl-con').on('tap', '#loging', function() {
	document.activeElement.blur();
	var phone = document.getElementById("account").value;

	var pass = document.getElementById("password").value;

	if(phone == "" || pass == "") {
		mui.toast('账号、密码不能为空');
		return;
	}
	var url = getApiUrl('http://www.jiaoyou0.cn', 'user/login');
	mui.ajax({
		type: "get",
		url: url,
		async: true,
		data: {
			'mobile': phone,
			'password': pass
		},
		success: function(data) {
			var data = JSON.parse(data)
			if(data.code == 1) {
				mui.toast(data.message)
				logindata(data.data, 'login')
			} else {
				mui.toast(data.message)
			}
		},
		error: function(err) {
			console.log(err.message)
		}
	});
})

//function checked() {
//	var ck1 = document.getElementById("checkbox_age").checked;
//	var ck2 = document.getElementById("checkbox").checked;
//	if(ck1 && ck2) {
//		return true;
//	} else {
//		return false;
//	}
//}
//点击注册按钮
mui('.button').on('tap', '#register', function() {
	console.log();
	if(!document.getElementById("checkbox").checked) {
		mui.toast('需要年满18周岁且同意协议');
		return
	}

	showWaiting('注册中…');
	var url = getApiUrl('http://www.jiaoyou0.cn', 'user/fastReg');
	//	var url = 'http://www.jiaoyou0.cn/index.php?s=index/user/fastReg&manager_id=85';
	mui.ajax({
		type: "post",
		dataType: 'json',
		url: url,
		async: true,
		success: function(data) {
			if(data.code == 1) {
				var nowtime = Date.parse(new Date()) / 1000;
				var content = '注册成功，您的用户名为“' + data.data.user_name + '”，密码为“123456”。作为新用户，我们已将您推荐给了100位女士！';
				var a = [];
				a.push({
					"send_uid": "1",
					"nick_name": "客服",
					"reply_type": 0,
					"headimg": "http://imgs.jiaoyou0.cn/uploads/kefu.jpg",
					"send_time": nowtime,
					"content": "您的帐户信息：",
					"yuedu": 1
				});
				var b = [];
				b.push({
					"send_uid": 1,
					"reply_type": 0,
					"content": content,
					"send_time": nowtime,
					"yuedu": 0,
					"nick_name": "客服",
					"headimg": "http://imgs.jiaoyou0.cn/uploads/kefu.jpg"
				});
				plus.storage.setItem('MsgData', JSON.stringify(a));
				plus.storage.setItem('IM1', JSON.stringify(b));
				setTimeout(function() {
					logindata(data.data, 'login')
					plus.nativeUI.closeWaiting();
				}, 2000)
			}
		},
		error: function(err) {
			plus.nativeUI.closeWaiting();
			mui.toast('出错')
		}
	})
});

mui('.mui-content').on('tap', '.denglu a', function() {
	mui.openWindow({
		url: 'html/my/XieYi.html',
		id: 'xieyi',
		extras: {
			type: 'xieyi'
		}
	})
});